package org.gvp.manager.actuator;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
import org.springframework.boot.actuate.endpoint.web.annotation.EndpointWebExtension;
import org.springframework.boot.actuate.info.InfoEndpoint;
import org.springframework.stereotype.Component;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.time.LocalDateTime;
import java.util.Map;

@Component
@EndpointWebExtension(endpoint = InfoEndpoint.class)
@RequiredArgsConstructor
public class InfoEndpointWebExtension {
    private final InfoEndpoint delegate ;
    @ReadOperation
    public WebEndpointResponse<Map> info(){
        Map<String,Object> info = this.delegate.info();
        info.put("currentTime", LocalDateTime.now());
        this.setProgressId(info);
        return new WebEndpointResponse<>(info,200);
    }

    private void setProgressId(Map<String,Object> map){
        ProcessHandle currentProcess = ProcessHandle.current();
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        String processName = runtimeMXBean.getName();
        long pid = Long.parseLong(processName.split("@")[0]);
        map.put("currentProcessId", currentProcess.pid());
        map.put("current_process_id", pid);
    }
}
